#ifndef _COMM_H_
#define _COMM_H_

#include<iostream>
#include<string>
#include<cassert>
#include<cstring>
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#include<fcntl.h>
#include"Log.hpp"

using namespace std;

#define PROJ_ID 888
#define MODE 0666
#define SIZE 4096
#define WRITE O_WRONLY
#define READ O_RDONLY

string PATH_NAME = "/home/snowdragon/";
string FIFO_PATH_NAME = "./fifo.ipc";

class Init
{
public:
    Init()
    {
        int ret = mkfifo(FIFO_PATH_NAME.c_str(), MODE);
        assert(ret == 0);
        Log("创建命名管道成功", Debug) << endl;;
    }
    ~Init()
    {
        int ret = unlink(FIFO_PATH_NAME.c_str());
        assert(ret == 0);
        Log("命名管道去关联成功", Debug) << endl;;
    }
};

//int OpenFifo(int flag)
int OpenFifo(string pasthName, int flag)        //添加文件名的参数，增加复用性
{
    //int fd = open(FIFO_NAME_NAME.c_str(), flag);
    int fd = open(pasthName.c_str(), flag);
    assert(fd != -1);
    Log("打开命名管道成功", Debug) << endl;;
    return fd;
}

void Wait(int fd)
{
    Log("等待中……", Notice) << endl;
    uint32_t tmp = 0;       //初始化，严谨
    ssize_t s = read(fd, &tmp, sizeof(uint32_t));
    assert(s == sizeof(uint32_t));
}

void WakeUp(int fd)
{
    uint32_t tmp = 1;
    ssize_t s = write(fd, &tmp, sizeof(uint32_t));
    assert(s == sizeof(uint32_t));
    Log("唤醒中……", Notice) << endl;;       //调整至最后，而不是最开始，因为是上方调用write之后才唤醒的
}

void CloseFifo(int fd)
{
    close(fd);
}

#endif